//: Playground - noun: a place where people can play

import UIKit
import Foundation

public final class CIImageKit<Base> {
    public let base: Base
    public init(_ base: Base) {
        self.base = base
    }
    
}
// protocol中 需要用 associatedtype 来预设一个类型
public protocol CIImageDownloaderProtocol {
    associatedtype type
    var ci: type { get }
}

public extension CIImageDownloaderProtocol {
    public var ci: CIImageKit<self> {
        get {
            return CIImageKit(self)
        }
    }
}

extension UIImageView: CIImageDownloaderProtocol{}

extension CIImageKit where Base: UIImageView {
    func setImage(url: URL, placeHolder: UIImage?) {
        // 实现 下载图片并缓存、展示的逻辑
    }
}

extension UIButton: CIImageDownloaderProtocol {}
extension CIImageKit where Base: UIButton {
    func setImage(url: URL, placeHolder: UIImage?) {
        // 实现 下载图片并缓存、展示的逻辑
    }
}

let image = UIImageView()
image.ci.setImage(url: URL.init(string: "https://www.manoboo.com")!, placeHolder: nil)

let button = UIButton(type: .custom)
button.ci.setImage(url: URL.init(string: "https://www.manoboo.com")!, placeHolder: nil)





